package com.hx.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hx.model.MessageBodyEntry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
@RequestMapping("/scan")
public class ScanController {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @GetMapping("/")
    public String index() {
        return "/index";
    }

    //广播式
    @GetMapping("/sendPublic")
    @ResponseBody
    public String sendPublic(String message) {
        message = "全局广播消息：" + message;
        simpMessagingTemplate.convertAndSend("/topic/public1", message);
        return message + "==>发送成功";
    }

    //一对一
    @GetMapping("/sendMessage")
    @ResponseBody
    public String sendMessage(String message) {
        // 以下是两种方式
        // /user/{userId}/**
        simpMessagingTemplate.convertAndSend("/user/userId123/msg",
                StrUtil.format("{}：{}[{}]", "一对一", message, "convertAndSend"));
        simpMessagingTemplate.convertAndSendToUser("userId123", "/msg",
                StrUtil.format("{}：{}[{}]", "一对一", message, "convertAndSendToUser"));
        simpMessagingTemplate.convertAndSendToUser("userId123", "/queue/test",
                StrUtil.format("{}：{}[{}]", "queue一对一", message, "convertAndSendToUser"));
        return message + "==>发送成功";
    }

    //接收前端send命令发送的
    @MessageMapping("/receiveClientMsg")
    //@SendTo("/user/userId123/msg")
    public MessageBodyEntry addUser(@Payload MessageBodyEntry chatMessage, StompHeaderAccessor headerAccessor) {
        // Add username in web socket session
        headerAccessor.getSessionAttributes().put("username", chatMessage.getSender());
        return chatMessage;
    }

    @SubscribeMapping("/init")
    public JSONObject subscribe() {
        /*ExecutorService executor = Executors.newFixedThreadPool(10);
        executor.submit(() -> {
            TimeUnit.SECONDS.sleep(2);
            JSONObject message = JSONUtil.createObj()
                    .putOpt("type", MessageBodyEntry.MessageType.CHAT)
                    .putOpt("sender", "后台用户")
                    .putOpt("content", "消息内容：初始化");
            return message;
        });*/
        return JSONUtil.createObj().putOpt("type", MessageBodyEntry.MessageType.CHAT)
                .putOpt("sender", "后台用户")
                .putOpt("content", "消息内容：初始化");
    }
}
